{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 4.16 \u8fed\u4ee3\u5668\u4ee3\u66ffwhile\u65e0\u9650\u5faa\u73af\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528 while \u5faa\u73af\u6765\u8fed\u4ee3\u5904\u7406\u6570\u636e\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u8c03\u7528\u67d0\u4e2a\u51fd\u6570\u6216\u8005\u548c\u4e00\u822c\u8fed\u4ee3\u6a21\u5f0f\u4e0d\u540c\u7684\u6d4b\u8bd5\u6761\u4ef6\u3002\n\u80fd\u4e0d\u80fd\u7528\u8fed\u4ee3\u5668\u6765\u91cd\u5199\u8fd9\u4e2a\u5faa\u73af\u5462\uff1f"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u4e2a\u5e38\u89c1\u7684IO\u64cd\u4f5c\u7a0b\u5e8f\u53ef\u80fd\u4f1a\u60f3\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "CHUNKSIZE = 8192\n\ndef reader(s):\n    while True:\n        data = s.recv(CHUNKSIZE)\n        if data == b'':\n            break\n        process_data(data)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u79cd\u4ee3\u7801\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528 iter() \u6765\u4ee3\u66ff\uff0c\u5982\u4e0b\u6240\u793a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def reader2(s):\n    for chunk in iter(lambda: s.recv(CHUNKSIZE), b''):\n        pass\n        # process_data(data)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u6000\u7591\u5b83\u5230\u5e95\u80fd\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u53ef\u4ee5\u8bd5\u9a8c\u4e0b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sys\nf = open('/etc/passwd')\nfor chunk in iter(lambda: f.read(10), ''):\n    n = sys.stdout.write(chunk)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "iter \u51fd\u6570\u4e00\u4e2a\u9c9c\u4e3a\u4eba\u77e5\u7684\u7279\u6027\u662f\u5b83\u63a5\u53d7\u4e00\u4e2a\u53ef\u9009\u7684 callable \u5bf9\u8c61\u548c\u4e00\u4e2a\u6807\u8bb0(\u7ed3\u5c3e)\u503c\u4f5c\u4e3a\u8f93\u5165\u53c2\u6570\u3002\n\u5f53\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u521b\u5efa\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c \u8fd9\u4e2a\u8fed\u4ee3\u5668\u4f1a\u4e0d\u65ad\u8c03\u7528 callable \u5bf9\u8c61\u76f4\u5230\u8fd4\u56de\u503c\u548c\u6807\u8bb0\u503c\u76f8\u7b49\u4e3a\u6b62\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u79cd\u7279\u6b8a\u7684\u65b9\u6cd5\u5bf9\u4e8e\u4e00\u4e9b\u7279\u5b9a\u7684\u4f1a\u88ab\u91cd\u590d\u8c03\u7528\u7684\u51fd\u6570\u5f88\u6709\u6548\u679c\uff0c\u6bd4\u5982\u6d89\u53ca\u5230I/O\u8c03\u7528\u7684\u51fd\u6570\u3002\n\u4e3e\u4f8b\u6765\u8bb2\uff0c\u5982\u679c\u4f60\u60f3\u4ece\u5957\u63a5\u5b57\u6216\u6587\u4ef6\u4e2d\u4ee5\u6570\u636e\u5757\u7684\u65b9\u5f0f\u8bfb\u53d6\u6570\u636e\uff0c\u901a\u5e38\u4f60\u5f97\u8981\u4e0d\u65ad\u91cd\u590d\u7684\u6267\u884c read() \u6216 recv() \uff0c\n\u5e76\u5728\u540e\u9762\u7d27\u8ddf\u4e00\u4e2a\u6587\u4ef6\u7ed3\u5c3e\u6d4b\u8bd5\u6765\u51b3\u5b9a\u662f\u5426\u7ec8\u6b62\u3002\u8fd9\u8282\u4e2d\u7684\u65b9\u6848\u4f7f\u7528\u4e00\u4e2a\u7b80\u5355\u7684 iter() \u8c03\u7528\u5c31\u53ef\u4ee5\u5c06\u4e24\u8005\u7ed3\u5408\u8d77\u6765\u4e86\u3002\n\u5176\u4e2d lambda \u51fd\u6570\u53c2\u6570\u662f\u4e3a\u4e86\u521b\u5efa\u4e00\u4e2a\u65e0\u53c2\u7684 callable \u5bf9\u8c61\uff0c\u5e76\u4e3a recv \u6216 read() \u65b9\u6cd5\u63d0\u4f9b\u4e86 size \u53c2\u6570\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}